package  
{
	import org.flixel.*;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Level0State extends PlayState 
	{
		[Embed(source = "../data/levels/Level 0.oel", mimeType = "application/octet-stream")] private var _newLevel:Class;
		
		private var _shownMovement:Boolean;
		private var _shownJump:Boolean;
		private var _shownChargeBulb:Boolean;
		private var _shownTime:Boolean;
		private var _shownBattery:Boolean;
		private var _shownElevator:Boolean;
		private var _Controls:FlxText;
		private var _flickerVoltmeter:Number;
		private var _lightBulbCounterLabel:FlxText;
		private var _voltsLabel:FlxText;
		private var _pauseLabel:FlxText;
		private var _muteLabel:FlxText;
		private var _timerLabel:FlxText;
		private var _fadeInLabels:Boolean = false;
		
		override public function create():void
		{			
			var background:FlxSprite = new FlxSprite(0, 0, ImageFiles.warehouseBackground);
			add(background);
			
			var backcrates:FlxSprite = new FlxSprite(0, FlxG.height, ImageFiles.warehouseBackCrates);
			backcrates.y -= backcrates.height;
			var frontcrates:FlxSprite = new FlxSprite(0, FlxG.height, ImageFiles.warehouseFrontCrates);
			frontcrates.y -= frontcrates.height;
			add(backcrates);
			add(frontcrates);
			
			var window:FlxSprite = new FlxSprite(FlxG.width / 4 - 78, 90, ImageFiles.warehouseWindow);
			add(window);
			window = new FlxSprite(FlxG.width / 2 - 39, 90, ImageFiles.warehouseWindow);
			add(window);
			window = new FlxSprite(3 * FlxG.width / 4, 90, ImageFiles.warehouseWindow);
			add(window);
			
			_lightBulbCounterLabel = new FlxText(0, 50, 75, "Lights Left").setFormat("PowellAntique", 14);
			_lightBulbCounterLabel.alpha = 0;
			add(_lightBulbCounterLabel);
			_voltsLabel = new FlxText(0, 30, FlxG.width, "Current Volts").setFormat("PowellAntique", 14, 0xffffff, "center");
			_voltsLabel.alpha = 0;
			add(_voltsLabel);
			_pauseLabel = new FlxText(130, 30, 50, "Pause").setFormat("PowellAntique", 14);
			_pauseLabel.alpha = 0;
			add(_pauseLabel);
			_muteLabel = new FlxText(470, 30, 50, "Mute").setFormat("PowellAntique", 14);
			_muteLabel.alpha = 0;
			add(_muteLabel);
			_timerLabel = new FlxText(570, 65, 70, "Time Left").setFormat("PowellAntique", 14);
			_timerLabel.alpha = 0;
			add(_timerLabel);
			
			super.create();
			
			_playerStartingX = 25;
			_playerStartingY = 25;
			_levelTime = 90;
			_flickerVoltmeter = 0;
			
			var roomXML:XML = new XML(new _newLevel);
			
			tilemap.loadOgmoTileMap(roomXML, "tiles", TilesetImages.warehouse);
			lightningmap.loadOgmoTileMap(roomXML, "tiles", TilesetImages.warehouse);
			objectmap.loadOgmoObjects(roomXML, "objects", "lights", LightBulb, lightBulbs);
			objectmap.loadOgmoObjects(roomXML, "objects", "batteries", Battery, powerups);
			objectmap.loadOgmoObjects(roomXML, "objects", "clocks", Clock, powerups);
			movermap.loadOgmoMovingObjects(roomXML, "objects", "movingPlatform", ImageFiles.elevator, moverGroup);
			
			tilemap.width = 640;
			
			_levelTransition.init();
			
			_Controls = new FlxText(80, 55, 350, "Use 'A' and 'D' to move");
			_Controls.setFormat("PowellAntique", 15, 0xFFFFFF, null, 0xFF000000);
			FlxG.followBounds(tilemap.left, tilemap.top, tilemap.right, tilemap.bottom);
			
			restartLevel();
			
			PowerGauge.currentVolts = 3;
			
			FlxG.playMusic(SoundFiles.warehouseMusic, 0.75);
			FlxG.music.fadeIn(1.0);
		}
		
		override public function update():void
		{
			if (!isUIVisible)
			{
				_Controls.visible = false;
			}
			else
			{
				_Controls.visible = true;
			}
			if (_fadeInLabels && _voltsLabel.alpha <= 1)
			{
				_lightBulbCounterLabel.alpha += FlxG.elapsed;
				_voltsLabel.alpha += FlxG.elapsed;
				_pauseLabel.alpha += FlxG.elapsed;
				_muteLabel.alpha += FlxG.elapsed;
				_timerLabel.alpha += FlxG.elapsed;
			}
			if (isLevelTransitioning)
			{
				if (whyLevelEnded != ALLLIGHTSLIT)
				{
					_levelTransition.ableToAdvance = false;
					_levelTransition.completeBanner.text = "INCOMPLETE";
				}
				else
				{
					LevelTransition.tutorialBeaten = true;
				}
			}
			super.update();
			if (!_shownMovement)
			{
				add(_Controls);
				_shownMovement = true;
			}
			if (playerTorso.y > 125 && !_shownChargeBulb)
			{
				_fadeInLabels = true;
				_Controls.text = "Click and hold to charge the light bulb";
				_Controls.x = 300;
				_Controls.y = 145;
				_shownChargeBulb = true;
			}
			if (lightBulbs.members[0].currentPowerLevel == 1)
			{
				_Controls.text = "Keep charging!";
			}
			if (lightBulbs.members[0].currentPowerLevel == 3 && moverGroup.members[1].y == 210)
			{
				_Controls.width = 300;
				_Controls.text = "Fully charged gets you more points, \nbut costs more volts";
				_Controls.x = 340;
				moverGroup.members[1].setVelocity(0, -250);
				moverGroup.members[1].setBoundingBox(0, 125);
			}
			if (moverGroup.members[1].velocity.y > 1)
			{
				moverGroup.members[1].setVelocity(0, 0);
			}
			if (playerTorso.y > 230 && !_shownJump)
			{
				_Controls.text = "Press 'W' to jump";
				_Controls.x = 40;
				_Controls.y = 250;
				_shownJump = true;
			}
			if (playerTorso.y > 330 && !_shownTime)
			{
				_Controls.text = "The watch will give you more time";
				_Controls.x = 60;
				_Controls.y = 330;
				_shownTime = true;
			}
			if (playerTorso.y > 405 && !_shownBattery)
			{
				_Controls.text = "The battery will restore your voltmeter";
				_Controls.x = 245;
				_Controls.y = 443;
				_shownBattery = true;
			}
			if (!powerups.members[0].exists && !_shownElevator)
			{
				_Controls.text = "Elevators will let you reach places \nyou can't get by yourself";
				_Controls.x = 300;
				_Controls.y = 410;
				_shownElevator = true;
			}
			if (!powerups.members[0].exists && playerTorso.y < 285)
			{
				_Controls.text = "Light all the light bulbs \nto complete the level";
				_Controls.x = 400;
				_Controls.y = 255;
				_Controls.width = 350;
			}
			if (!timerDisplay.isTimeLeft() || lightBulbsDisplay.litCount == lightBulbsDisplay.totalLightbulbs())
			{
				_Controls.text = "";
			}
		}
		
	}

}